package com.zcxy.fast.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ReflectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcxy.fast.bean.Prop;
import com.zcxy.fast.entity.SysComponet;
import com.zcxy.fast.entity.SysComponetItem;
import com.zcxy.fast.mapper.SysComponetItemMapper;
import com.zcxy.fast.mapper.SysComponetMapper;
import com.zcxy.fast.mybatisExtend.BaseServiceImpl;
import com.zcxy.fast.service.ISysComponetItemService;
import com.zcxy.fast.utils.BeanCopyUtil;
import com.zcxy.fast.utils.StringUtils;
import com.zcxy.fast.vo.SysComponetItemVo;
import io.swagger.annotations.ApiModelProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;


/**
 * 组件明细配置表业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2021-01-21 15:07:23
 */
@Service
@Slf4j
public class SysComponetItemServiceImpl extends BaseServiceImpl<SysComponetItemMapper, SysComponetItem> implements ISysComponetItemService {

    @Autowired
    private SysComponetMapper sysComponetMapper;

    @Override
    public List<SysComponetItemVo> queryList(SysComponetItem sysComponetItem) {
        SysComponetItem sysComponetItemq = sysComponetItem == null ? new SysComponetItem() : sysComponetItem;
        QueryWrapper queryWrapper = new QueryWrapper(sysComponetItemq);
        queryWrapper.orderByAsc("sort");
        List<SysComponetItemVo> sysComponetItemVos = BeanCopyUtil.copyListToList(baseMapper.selectList(queryWrapper), SysComponetItemVo.class);
        if (CollectionUtil.isEmpty(sysComponetItemVos)) {
            SysComponet sysComponet = sysComponetMapper.selectById(sysComponetItem.getSysComponentId());

            List<SysComponetItem> sysComponetItems = new ArrayList<>();
            try {
                Field[] fields = ReflectUtil.getFields(Class.forName(sysComponet.getApplyTable()));
                int i = 1;
                for (Field field : fields) {
                    ApiModelProperty apiModelProperty = field.getAnnotation(ApiModelProperty.class);
                    SysComponetItem item = new SysComponetItem();
                    item.setColumnName(field.getName());
                    if (apiModelProperty != null) {
                        item.setColumnComment(apiModelProperty.value());
                    }
                    item.setNeedShow("0");
                    item.setSort(i);
                    item.setSysComponentId(sysComponet.getId());
                    item.setJavaname(field.getName());
                    i++;
                    sysComponetItems.add(item);
                }
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
            sysComponetItemVos = BeanCopyUtil.copyListToList(sysComponetItems, SysComponetItemVo.class);
        }
        return sysComponetItemVos;
    }

    @Override
    public void updateSysComponetItems(List<SysComponetItem> sysComponetItems) {
        if (CollectionUtil.isNotEmpty(sysComponetItems)) {

             for(int i=0;i<sysComponetItems.size();i++){
                 sysComponetItems.get(i).setSort(i+1);
             }
            saveOrUpdateBatch(sysComponetItems);
            SysComponet sysComponet = sysComponetMapper.selectById(sysComponetItems.get(0).getSysComponentId());
            List<Prop> props = new ArrayList<>();
            sysComponetItems.forEach(i -> {
                if ("1".equals(i.getNeedShow())) {
                    Prop prop = new Prop();
                    prop.setLabel(i.getColumnComment());
                    prop.setProp(i.getJavaname());
                    props.add(prop);
                }
            });
            String s = JSONUtil.toJsonStr(props);
            sysComponet.setProps(s);
            sysComponetMapper.updateById(sysComponet);
        }


    }


}
